package com.wuxiaoge.job;

import com.wuxiaoge.decider.MyDecider;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.job.builder.JobBuilder;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.step.builder.StepBuilder;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.support.JdbcTransactionManager;
import org.springframework.stereotype.Component;

@Component
public class DeciderJobDemo {

    @Autowired
    private MyDecider myDecider;

    @Bean
    public Job deciderJob(JobRepository jobRepository, JdbcTransactionManager transactionManager) {
       return new JobBuilder("deciderJob", jobRepository)
                .start(step1(jobRepository,transactionManager))
                .next(myDecider)
                .from(myDecider).on("weekend").to(step2(jobRepository,transactionManager))
                .from(myDecider).on("workingDay").to(step3(jobRepository,transactionManager))
                .from(step3(jobRepository,transactionManager)).on("*").to(step4(jobRepository,transactionManager))
                .end()
                .build();
    }

    private Step step1(JobRepository jobRepository, JdbcTransactionManager transactionManager) {

        return new StepBuilder("step1", jobRepository)
                .tasklet((contribution, chunkContext) -> {
                    System.out.println(Thread.currentThread().getName()+"执行步骤一操作。。。");
                    return RepeatStatus.FINISHED;
                },transactionManager).build();
    }

    private Step step2(JobRepository jobRepository, JdbcTransactionManager transactionManager) {
        return new StepBuilder("step2", jobRepository)
                .tasklet((contribution, chunkContext) -> {
                    System.out.println(Thread.currentThread().getName()+"执行步骤二操作。。。");
                    return RepeatStatus.FINISHED;
                },transactionManager).build();
    }

    private Step step3(JobRepository jobRepository, JdbcTransactionManager transactionManager) {

        return new StepBuilder("step3", jobRepository)
                .tasklet((contribution, chunkContext) -> {
                    System.out.println(Thread.currentThread().getName()+"执行步骤三操作。。。");
                    return RepeatStatus.FINISHED;
                },transactionManager).build();
    }


    private Step step4(JobRepository jobRepository, JdbcTransactionManager transactionManager) {

        return new StepBuilder("step4", jobRepository)
                .tasklet((contribution, chunkContext) -> {
                    System.out.println(Thread.currentThread().getName()+"执行步骤四操作。。。");
                    return RepeatStatus.FINISHED;
                },transactionManager).build();
    }

}
